package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("itemCatController")
public class ItemCatController {
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private TemplateService templateService;
    //跳转分类管理
    @RequestMapping("toItemCat")
    public String toItemCat(Model model){
        return "admin/item_cat";
    }

    //查询所有分类管理
    @RequestMapping("findAll")
    @ResponseBody
    public StatusCode findAll(@RequestParam(defaultValue = "0") Long id){
        List<TbItemCat> list = itemCatService.findAll(id);
        if (list == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(list);
    }

    //分类列表
    @RequestMapping("returnfindAll")
    @ResponseBody
    public StatusCode returnfindAll(Long parent){
        if (parent == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        List<TbItemCat> tbItemCat = itemCatService.selectById(parent);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbItemCat);
    }

    //增加
    @RequestMapping("insertItemCat")
    @ResponseBody
    public StatusCode insertItemCat(TbItemCat tbItemCat){
        if (tbItemCat == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = itemCatService.insertItemCat(tbItemCat);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }

        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }


    //回显
    @RequestMapping("echoItemCat")
    @ResponseBody
    public StatusCode echoItemCat(Long id){
        if (id == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        TbItemCat tbItemCat = itemCatService.echoItemCatById(id);
        if (tbItemCat == null){
            return StatusCode.build(ConstantEnum.ERROR);
        }
        return StatusCode.build(ConstantEnum.SUCCESS).setT(tbItemCat);
    }

    //修改
    @RequestMapping("updateItemCat")
    @ResponseBody
    public StatusCode updateItemCat(TbItemCat tbItemCat){
        if (tbItemCat == null){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = itemCatService.updateItemCat(tbItemCat);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }

        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }

    //删除
    @RequestMapping("deleteById")
    @ResponseBody
    public StatusCode deleteById(Long[] ids){
        if (ids.length <= 0){
            return StatusCode.build(ConstantEnum.NULL_ERROR);
        }
        Boolean b = itemCatService.deleteById(ids);
        if (!b){
            return StatusCode.build(ConstantEnum.UPDATE_ERROR);
        }

        return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
    }
}
